package com.abp.controller;


import com.abp.entity.Book;
import com.abp.entity.BookLend;
import com.abp.entity.BookLibary;
import com.abp.entity.BookReback;
import com.abp.entity.view.BookLendView;
import com.abp.qpage.QPage;
import com.abp.service.BookService;
import com.abp.service.IBookLendService;
import com.abp.service.IBookLibaryService;
import com.abp.service.IBookRebackService;
import com.abp.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 * 图书借出记录表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-02-14
 */
@Controller
@RequestMapping("/api/admin/book-lend")
public class BookLendController {

    @Autowired
    private IBookLendService service;

    /**
     * 路由地址
     */
    public String prefix = "/view/admin/booklend";


    /**
     * 进入管理页面
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return prefix+"/index";
    }


    /**
     * 获取所有的列表
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Result getList(QPage qPage, BookLendView obj) {
        return Result.reOk("查询成功",this.service.getPageList(qPage,obj));
    }


    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping("/add")
    public String add(){
        return prefix+"/add";
    }

    /**
     * 新增保存
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(BookLend obj) {
        return this.service.insert(obj);
    }

    /**
     * 进入编辑页面
     * @return
     */
    @RequestMapping("/edit")
    public String edit(){
        return prefix+"/edit";
    }

    /**
     * 根据ID获取信息
     * @param id
     * @return
     */
    @RequestMapping("/getById")
    @ResponseBody
    public Result getById(Integer id) {
        BookLend obj=this.service.getById(id);
        return Result.reOk("查询成功",obj);
    }

    /**
     * 编辑保存
     * @return
     */
    @RequestMapping("/updates")
    @ResponseBody
    public Result updates(BookLend obj) {
        return this.service.updates(obj);
    }

    @Autowired
    private BookService bookService;

    @Autowired
    private IBookRebackService bookRebackService;

    /**
     * 归还图书
     * @return
     */
    @RequestMapping("/rebackId")
    @ResponseBody
    public Result rebackId(int id) {
        BookLend bookLend=this.service.getById(id);
        if(bookLend.getLendStatus()==2){
            return Result.reError("请不要重复归还图书");
        }
        bookLend.setLendStatus(2);
        bookLend.setRebackTime(new Date());
        this.service.updateById(bookLend);
        BookReback bookReback=new BookReback();
        bookReback.setUserId(bookLend.getUserId());
        bookReback.setBookId(bookLend.getBookId());
        bookReback.setRebackTime(new Date());
        this.bookRebackService.save(bookReback);
        Book book=bookService.getById(bookLend.getBookId());
        book.setStatus(1);
        this.bookService.updateById(book);
        return Result.reOk("归还成功");
    }


}
